home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SATAN11.ZIP / SRC / RPCGEN / RPC_SVCO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-08  |  7.5 KB  |  281 lines

  1. /* @(#)rpc_svcout.c    2.1 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #ifndef lint
  31. static char sccsid[] = "@(#)rpc_svcout.c 1.6 87/06/24 (C) 1987 SMI";
  32. #endif
  33.  
  34. /*
  35.  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
  36.  * Copyright (C) 1987, Sun Microsytsems, Inc. 
  37.  */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <string.h>
  42. #include "rpc_parse.h"
  43. #include "rpc_util.h"
  44.  
  45. static char RQSTP[] = "rqstp";
  46. static char TRANSP[] = "transp";
  47. static char ARG[] = "argument";
  48. static char RESULT[] = "result";
  49. static char ROUTINE[] = "local";
  50.  
  51. static write_program();
  52. static printerr();
  53. static printif();
  54.  
  55. /*
  56.  * write most of the service, that is, everything but the registrations. 
  57.  */
  58. void
  59. write_most()
  60. {
  61.     list *l;
  62.     definition *def;
  63.     version_list *vp;
  64.  
  65.     for (l = defined; l != NULL; l = l->next) {
  66.         def = (definition *) l->val;
  67.         if (def->def_kind == DEF_PROGRAM) {
  68.             for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  69.                 f_print(fout, "\nstatic void ");
  70.                 pvname(def->def_name, vp->vers_num);
  71.                 f_print(fout, "();");
  72.             }
  73.         }
  74.     }
  75.     f_print(fout, "\n\n");
  76.     f_print(fout, "main()\n");
  77.     f_print(fout, "{\n");
  78.     f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
  79.     f_print(fout, "\n");
  80.     for (l = defined; l != NULL; l = l->next) {
  81.         def = (definition *) l->val;
  82.         if (def->def_kind != DEF_PROGRAM) {
  83.             continue;
  84.         }
  85.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  86.              f_print(fout, "\t(void)pmap_unset(%s, %s);\n", def->def_name, vp->vers_name);
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  * write a registration for the given transport 
  94.  */
  95. void
  96. write_register(transp)
  97.     char *transp;
  98. {
  99.     list *l;
  100.     definition *def;
  101.     version_list *vp;
  102.  
  103.     f_print(fout, "\n");
  104.     f_print(fout, "\t%s = svc%s_create(RPC_ANYSOCK", TRANSP, transp);
  105.     if (streq(transp, "tcp")) {
  106.         f_print(fout, ", 0, 0");
  107.     }
  108.     f_print(fout, ");\n");
  109.     f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
  110.      f_print(fout, "\t\t(void)fprintf(stderr, \"cannot create %s service.\\n\");\n", transp);
  111.     f_print(fout, "\t\texit(1);\n");
  112.     f_print(fout, "\t}\n");
  113.  
  114.     for (l = defined; l != NULL; l = l->next) {
  115.         def = (definition *) l->val;
  116.         if (def->def_kind != DEF_PROGRAM) {
  117.             continue;
  118.         }
  119.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  120.             f_print(fout,
  121.                 "\tif (!svc_register(%s, %s, %s, ",
  122.                 TRANSP, def->def_name, vp->vers_name);
  123.             pvname(def->def_name, vp->vers_num);
  124.             f_print(fout, ", IPPROTO_%s)) {\n",
  125.                 streq(transp, "udp") ? "UDP" : "TCP");
  126.             f_print(fout,
  127.                  "\t\t(void)fprintf(stderr, \"unable to register (%s, %s, %s).\\n\");\n",
  128.                 def->def_name, vp->vers_name, transp);
  129.             f_print(fout, "\t\texit(1);\n");
  130.             f_print(fout, "\t}\n");
  131.         }
  132.     }
  133. }
  134.  
  135.  
  136. /*
  137.  * write the rest of the service 
  138.  */
  139. void
  140. write_rest()
  141. {
  142.     f_print(fout, "\tsvc_run();\n");
  143.      f_print(fout, "\t(void)fprintf(stderr, \"svc_run returned\\n\");\n");
  144.     f_print(fout, "\texit(1);\n");
  145.     f_print(fout, "}\n");
  146. }
  147.  
  148. void
  149. write_programs(storage)
  150.     char *storage;
  151. {
  152.     list *l;
  153.     definition *def;
  154.  
  155.     for (l = defined; l != NULL; l = l->next) {
  156.         def = (definition *) l->val;
  157.         if (def->def_kind == DEF_PROGRAM) {
  158.             write_program(def, storage);
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. static
  165. write_program(def, storage)
  166.     definition *def;
  167.     char *storage;
  168. {
  169.     version_list *vp;
  170.     proc_list *proc;
  171.     int filled;
  172.  
  173.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  174.         f_print(fout, "\n");
  175.         if (storage != NULL) {
  176.             f_print(fout, "%s ", storage);
  177.         }
  178.         f_print(fout, "void\n");
  179.         pvname(def->def_name, vp->vers_num);
  180.         f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
  181.         f_print(fout, "    struct svc_req *%s;\n", RQSTP);
  182.         f_print(fout, "    SVCXPRT *%s;\n", TRANSP);
  183.         f_print(fout, "{\n");
  184.  
  185.         filled = 0;
  186.         f_print(fout, "\tunion {\n");
  187.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  188.             if (streq(proc->arg_type, "void")) {
  189.                 continue;
  190.             }
  191.             filled = 1;
  192.             f_print(fout, "\t\t");
  193.             ptype(proc->arg_prefix, proc->arg_type, 0);
  194.             pvname(proc->proc_name, vp->vers_num);
  195.             f_print(fout, "_arg;\n");
  196.         }
  197.         if (!filled) {
  198.             f_print(fout, "\t\tint fill;\n");
  199.         }
  200.         f_print(fout, "\t} %s;\n", ARG);
  201.         f_print(fout, "\tchar *%s;\n", RESULT);
  202.         f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
  203.         f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
  204.         f_print(fout, "\n");
  205.         f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
  206.  
  207.         if (!nullproc(vp->procs)) {
  208.             f_print(fout, "\tcase NULLPROC:\n");
  209.              f_print(fout, "\t\t(void)svc_sendreply(%s, xdr_void, (char *)NULL);\n", TRANSP);
  210.             f_print(fout, "\t\treturn;\n\n");
  211.         }
  212.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  213.             f_print(fout, "\tcase %s:\n", proc->proc_name);
  214.             f_print(fout, "\t\txdr_%s = xdr_%s;\n", ARG, 
  215.                 stringfix(proc->arg_type));
  216.             f_print(fout, "\t\txdr_%s = xdr_%s;\n", RESULT, 
  217.                 stringfix(proc->res_type));
  218.             f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
  219.             pvname(proc->proc_name, vp->vers_num);
  220.             f_print(fout, ";\n");
  221.             f_print(fout, "\t\tbreak;\n\n");
  222.         }
  223.         f_print(fout, "\tdefault:\n");
  224.         printerr("noproc", TRANSP);
  225.         f_print(fout, "\t\treturn;\n");
  226.         f_print(fout, "\t}\n");
  227.  
  228.          f_print(fout, "\tmemset((char *)&%s, 0, sizeof(%s));\n", ARG, ARG);
  229.         printif("getargs", TRANSP, "&", ARG);
  230.         printerr("decode", TRANSP);
  231.         f_print(fout, "\t\treturn;\n");
  232.         f_print(fout, "\t}\n");
  233.  
  234.         f_print(fout, "\t%s = (*%s)(&%s, %s);\n", RESULT, ROUTINE, ARG,
  235.             RQSTP);
  236.         f_print(fout, 
  237.             "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
  238.             RESULT, TRANSP, RESULT, RESULT);
  239.         printerr("systemerr", TRANSP);
  240.         f_print(fout, "\t}\n");
  241.  
  242.         printif("freeargs", TRANSP, "&", ARG);
  243.          f_print(fout, "\t\t(void)fprintf(stderr, \"unable to free arguments\\n\");\n");
  244.         f_print(fout, "\t\texit(1);\n");
  245.         f_print(fout, "\t}\n");
  246.  
  247.         f_print(fout, "}\n\n");
  248.     }
  249. }
  250.  
  251. static
  252. printerr(err, transp)
  253.     char *err;
  254.     char *transp;
  255. {
  256.     f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
  257. }
  258.  
  259. static
  260. printif(proc, transp, prefix, arg)
  261.     char *proc;
  262.     char *transp;
  263.     char *prefix;
  264.     char *arg;
  265. {
  266.     f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
  267.         proc, transp, arg, prefix, arg);
  268. }
  269.  
  270.  
  271. nullproc(proc)
  272.     proc_list *proc;
  273. {
  274.     for (; proc != NULL; proc = proc->next) {
  275.         if (streq(proc->proc_num, "0")) {
  276.             return (1);
  277.         }
  278.     }
  279.     return (0);
  280. }
  281.